﻿using Games.Helper.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WK.Libraries.BetterFolderBrowserNS.Helpers;

namespace Games_0711
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string lastSelectPath = string.Empty;
        List<ConfigJsonModel> Configs= new List<ConfigJsonModel>();
        public MainWindow()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
            this.btnClose.Click += (se, ee) => {
                this.Close();
            };
            this.btnMax.Checked += (se, ee) => {
                this.WindowState = WindowState.Maximized;
                this.btnMax.Content = "\ue621";
            };
            this.btnMax.Unchecked += (se, ee) => {
                this.WindowState = WindowState.Normal;
                this.btnMax.Content = "\ue690";
            };
            this.btnMin.Click += (se, ee) => {
                this.WindowState = WindowState.Minimized;
            };
            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
            this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
            this.btnOpen.Click += BtnOpen_Click;
        }

        private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            BetterFolderBrowserDialog folderBrowserDialog = new BetterFolderBrowserDialog();
            folderBrowserDialog.Title = "选择文件夹";
            folderBrowserDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            folderBrowserDialog.ShowDialog();
            if (folderBrowserDialog.FileName == string.Empty) {
                MessageBox.Show("请选择文件夹");
                return;
            }
            Configs.Clear();
            text.Text = folderBrowserDialog.FileName;
            OutputText.Text = string.Empty;
            listDirectory(folderBrowserDialog.FileName);
            OutputText.Text = ConvertJsonString(JsonConvert.SerializeObject(Configs));
        }

        private void listDirectory(string path)
        {
            string gameName = string.Empty;
            try {
                DirectoryInfo theFolder = new DirectoryInfo(@path);
                foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) {
                    gameName = NextFolder.Name;
                    foreach (DirectoryInfo SecondFolder in NextFolder.GetDirectories()) {
                        if ("config" == SecondFolder.Name.ToLower()) {
                            foreach (FileInfo NextFile in SecondFolder.GetFiles()) {
                                if (NextFile.Name.EndsWith(".json")) {
                                    string json = File.ReadAllText(NextFile.FullName);
                                    ConfigJsonModel data = JsonConvert.DeserializeObject<ConfigJsonModel>(json);
                                    Configs.Add(data);
                                }
                            }
                            //Configs.Add(new ConfigJsonModel() {
                            //    title = "ddd",
                            //    description = "666"
                            //});
                            continue;
                        }
                    }
                }
            } catch (Exception ex) {
                MessageBox.Show(gameName + "__________________"+ ex.Message);
            }
        }

        public string ConvertJsonString(string json)
        {
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(json);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null) {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) {
                    Formatting = Formatting.Indented,
                    //数据内换行推进字符数量
                    Indentation = 2,
                    //key: value (冒号后的字符)
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            } else {
                return json;
            }
        }
    }
}
